package cn.tedu.baidashi.service.Impl;

import cn.tedu.baidashi.exception.ServiceException;
import cn.tedu.baidashi.mapper.OrderAddressMapper;
import cn.tedu.baidashi.model.Address;
import cn.tedu.baidashi.model.City;
import cn.tedu.baidashi.model.Province;
import cn.tedu.baidashi.model.Zone;
import cn.tedu.baidashi.service.IOrderAddressService;
import cn.tedu.baidashi.vo.AddressVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2022-02-19
 */
@Service
public class OrderAddressServiceImpl extends ServiceImpl<OrderAddressMapper, Address> implements IOrderAddressService {
    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Override
    public List<Address> getAddressesByUserName(String username) {
        return orderAddressMapper.getAddressesByUserName(username);
    }

    @Override
    public Address addAddress(AddressVo addressVO, String username) {
        Address address=new Address()
                .setCustomerName(addressVO.getCustomerName())
                .setProvinceName(addressVO.getProvinceName())
                .setCityName(addressVO.getCityName())
                .setZoneName(addressVO.getZoneName())
                .setPhone(addressVO.getPhone())
                .setPostcode(addressVO.getPostcode())
                .setStreet(addressVO.getStreet())
                .setCustomerId(2);
        int num=orderAddressMapper.insert(address);
        if(num!=1){
            throw new ServiceException("数据库异常");
        }
        return address;
    }

    @Override
    public Address getAddressByUserNameAndAddressId(Integer addressId, String username){
        Address address=orderAddressMapper.getAddressByUserNameAndAddressId(addressId,username);
        if(address==null){
            throw new ServiceException("用户没有该地址");
        }
        return address;
    }

    @Override
    public Address updateAddress(Integer addressId, AddressVo addressVO, String username) {
        Address address=getAddressByUserNameAndAddressId(addressId,username);
        address.setCustomerName(addressVO.getCustomerName())
                .setProvinceName(addressVO.getProvinceName())
                .setCityName(addressVO.getCityName())
                .setZoneName(addressVO.getZoneName())
                .setPhone(addressVO.getPhone())
                .setStreet(addressVO.getStreet())
                .setPostcode(addressVO.getPostcode());
        int num=orderAddressMapper.updateById(address);
        if(num!=1){
            throw new ServiceException("数据库异常！");
        }
        return address;
    }

    @Override
    public List<Province> getProvinces() {
        return orderAddressMapper.getAllProvinces();
    }

    @Override
    public List<City> getCitysByProvinceId(Integer provinceId) {
        return orderAddressMapper.getCitysByProvinceId(provinceId);
    }

    public List<City> getCitysByProvinceName(String provinceName) {
        return orderAddressMapper.getCitysByProvinceName(provinceName);
    }

    @Override
    public List<Zone> getZonesByCityId(Integer cityId) {
        return orderAddressMapper.getZonesByCityId(cityId);
    }

    public List<Zone> getZonesByCityName(String cityName) {
        return orderAddressMapper.getZonesByCityName(cityName);
    }
}
